package view.inventory;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableRowSorter;
import main.ServicesList;
import model.inventory.Dept;
import model.inventory.Item;
import model.inventory.Principal;
import model.publics.Supplier;
import utility.Constants;
import utility.NameValueList;
import utility.com.ColumnResizer;
import utility.com.ComponentUtils;
import utility.typeSafe.EnumUtils;
import utility.typeSafe.ItemPerfoma;
import view.RekapTemplate;
import view.lookup.LookupDeptFrm;
import view.lookup.LookupSimpleFrm;

public class ItemListFrm extends RekapTemplate {
    
    private final int COL_NO = 0;
    private final int COL_MAX = 7;
    private final int COL_MIN = 8;
    private final int COL_MINX = 9;
    private TableCellRenderer header=null;
    
    private ItemTM tableModel;
    private List<Item> items;
    private Supplier supplier;
    private Principal principal;
    private Dept dept;
    private TableRowSorter<ItemTM> sorter;
    
    private List<NameValueList> performList = EnumUtils.enumToNameValueList(ItemPerfoma.class);
    private Object oldValue, newValue;
    private ItemListener comboboxListener = new ComboboxListener();
    
    public ItemListFrm() {
        initComponents();
        txtBarcode.addKeyListener(new ActionKeyListener());
        txtPLU.addKeyListener(new ActionKeyListener());
        txtNama.addKeyListener(new ActionKeyListener());
        txtKategori.addKeyListener(new ActionKeyListener());
        txtPrincipal.addKeyListener(new ActionKeyListener());
        txtSupplier.addKeyListener(new ActionKeyListener());
        txtBarcode.addFocusListener(new ActionFocusGained());
        txtPLU.addFocusListener(new ActionFocusGained());
        txtNama.addFocusListener(new ActionFocusGained());
        txtKategori.addFocusListener(new ActionFocusGained());
        txtPrincipal.addFocusListener(new ActionFocusGained());
        txtSupplier.addFocusListener(new ActionFocusGained());
    }
    
    private void lookupMaster(int purpose, String param) {
        Object object;
        if (purpose==Constants.LOOKUP_DEPT) {
            dept = (Dept) new LookupDeptFrm().execute();
        } else if (purpose==Constants.LOOKUP_PRINCIPAL) {
            principal = (Principal) new LookupSimpleFrm().execute(purpose, param);
        } else if (purpose==Constants.LOOKUP_SUPPLIER) {
            supplier = (Supplier) new LookupSimpleFrm().execute(purpose, param);
        }
                
        setMaster(purpose);
    }
    
    private void setMaster(int purpose) {
        switch (purpose) {
            case Constants.LOOKUP_DEPT:
                if(dept!=null) {
                    txtKategori.setText(dept.getNama());
                    loadData();
                }break;
                                        
            case Constants.LOOKUP_PRINCIPAL:
                if(principal!=null) {
                    txtPrincipal.setText(principal.getNama());
                    loadData();
                }break;
                    
            case Constants.LOOKUP_SUPPLIER:
                if(supplier!=null) {
                    txtSupplier.setText(supplier.getNama());
                    loadData();
                } break;
            default: break;    
        }
    }
    
    private void getMaster(int purpose, String kode) {
        switch (purpose) {
           case Constants.LOOKUP_DEPT:
               dept = ServicesList.getDeptService().getDept(kode);
               if (dept != null) { setMaster(purpose);
               }else { lookupMaster(purpose, txtKategori.getText()); }
               break;
               
           case Constants.LOOKUP_SUPPLIER:
               supplier = ServicesList.getSupplierService().getSupplier(kode);
               if (supplier != null) { setMaster(purpose);
               }else { lookupMaster(purpose, txtSupplier.getText()); }
               break;
               
           case Constants.LOOKUP_PRINCIPAL:
               principal = ServicesList.getPrincipalService().getPrincipal(kode);
               if (principal != null) { setMaster(purpose);
               }else { lookupMaster(purpose, txtPrincipal.getText()); }
               break;
           default: break;    
        }
    }
    
    class ActionKeyListener implements KeyListener {
        @Override public void keyTyped(KeyEvent e) {} 
        @Override public void keyReleased(KeyEvent e) {}
        
        @Override public void keyPressed(KeyEvent e) {
            switch (e.getKeyCode()) { 
                case KeyEvent.VK_F2: 
                    switch(e.getComponent().getName()) {
                        case "txtKategori": lookupMaster(Constants.LOOKUP_DEPT, txtKategori.getText()); break;
                        case "txtPrincipal": lookupMaster(Constants.LOOKUP_PRINCIPAL, txtPrincipal.getText()); break;
                        case "txtSupplier": lookupMaster(Constants.LOOKUP_SUPPLIER, txtSupplier.getText()); break;
                    };
                break;  
                    
                case KeyEvent.VK_ENTER: 
                    switch(e.getComponent().getName()) {
                        case "txtBarcode": 
                            newValue = txtBarcode.getText();
                            if (!newValue.equals(oldValue)) { loadData(); }
                            break;
                            
                        case "txtNama": 
                            newValue = txtNama.getText();
                            if (!newValue.equals(oldValue)) { loadData(); }
                            break;
                            
                        case "txtPLU": 
                            newValue = txtPLU.getText();
                            if (!newValue.equals(oldValue)) { loadData(); }
                            break;    
                        
                        case "txtKategori": 
                            newValue = txtKategori.getText();
                            if (!newValue.equals(oldValue)) { getMaster(Constants.LOOKUP_DEPT, txtKategori.getText()); }
                            break;
                            
                        case "txtPrincipal": 
                            newValue = txtPrincipal.getText();
                            if (!newValue.equals(oldValue)) { getMaster(Constants.LOOKUP_PRINCIPAL, txtPrincipal.getText()); }
                            break;   
                            
                        case "txtSupplier": 
                            newValue = txtSupplier.getText();
                            if (!newValue.equals(oldValue)) { getMaster(Constants.LOOKUP_SUPPLIER, txtSupplier.getText()); }
                            break;       
                    };
                break;      
            }
        }
    }
    
    class ActionFocusGained implements FocusListener {
        @Override public void focusGained(FocusEvent e) { 
            Component componen = (Component) e.getSource(); 
            if (componen instanceof JTextField) { oldValue = ((JTextField) componen).getText();
            }else if (componen instanceof JComboBox) { oldValue = ((JComboBox) componen).getSelectedIndex();
            }else if (componen instanceof JCheckBox) { oldValue = ((JCheckBox) componen).isSelected();
            }else if (componen instanceof JTextArea) { oldValue = ((JTextArea) componen).getText(); }
        }
        
        @Override public void focusLost(FocusEvent e) {
            Component componen = (Component) e.getSource(); 
            if (componen instanceof JTextField) { newValue = ((JTextField) componen).getText();
            }else if (componen instanceof JComboBox) { newValue = ((JComboBox) componen).getSelectedIndex();
            }else if (componen instanceof JCheckBox) { newValue = ((JCheckBox) componen).isSelected();
            }else if (componen instanceof JTextArea) { oldValue = ((JTextArea) componen).getText(); }
        }
    }

    class ComboboxListener implements ItemListener {
        @Override
        public void itemStateChanged(ItemEvent e) {
            if(e.getStateChange()==ItemEvent.SELECTED) {
                loadData();
            }
        }
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar1 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        txtPrincipal = new javax.swing.JTextField();
        txtPLU = new javax.swing.JTextField();
        txtBarcode = new javax.swing.JTextField();
        txtKategori = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtSupplier = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtNama = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        cmbTag = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        cmbTax = new javax.swing.JComboBox();
        cmbHalal = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        grid = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setMaximumSize(new java.awt.Dimension(32767, 20));
        jToolBar1.setSize(this.getWidth(), 50);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edit_add.png"))); // NOI18N
        jButton1.setText("Tambah");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton1);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edit.png"))); // NOI18N
        jButton2.setText("Edit");
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton2);

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edit_remove.png"))); // NOI18N
        jButton3.setText("Hapus");
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton3);

        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/reload.png"))); // NOI18N
        jButton5.setText("Load");
        jButton5.setFocusable(false);
        jButton5.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton5.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });
        jToolBar1.add(jButton5);

        btnReset.setText("Reset");
        btnReset.setFocusable(false);
        btnReset.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnReset.setName("btnReset"); // NOI18N
        btnReset.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });
        jToolBar1.add(btnReset);

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/print.png"))); // NOI18N
        jButton4.setText("Print");
        jButton4.setFocusable(false);
        jButton4.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        jButton4.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton4);

        jSplitPane1.setDividerLocation(0);
        jSplitPane1.setDividerSize(10);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setLastDividerLocation(100);
        jSplitPane1.setOneTouchExpandable(true);

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel1.setPreferredSize(new java.awt.Dimension(14, 50));

        txtPrincipal.setName("txtPrincipal"); // NOI18N

        txtPLU.setName("txtPLU"); // NOI18N

        txtBarcode.setName("txtBarcode"); // NOI18N

        txtKategori.setName("txtKategori"); // NOI18N

        jLabel1.setText("Kategori :");

        jLabel2.setText("Supplier :");

        txtSupplier.setName("txtSupplier"); // NOI18N

        jLabel3.setText("Principal :");

        jLabel4.setText("PLU :");

        jLabel5.setText("Barcode :");

        txtNama.setName("txtNama"); // NOI18N

        jLabel6.setText("Nama :");

        cmbTag.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbTag.setName("cmbTag"); // NOI18N

        jLabel7.setText("Tag :");

        cmbTax.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Semua", "BKP", "NON BKP" }));
        cmbTax.setName("cmbTax"); // NOI18N

        cmbHalal.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Semua", "Halal", "Tdk Halal" }));
        cmbHalal.setName("cmbHalal"); // NOI18N

        jLabel8.setText("BKP:");

        jLabel9.setText("Halal :");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtKategori, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSupplier, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(cmbTag, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(txtPLU, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtBarcode, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(txtNama))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cmbTax, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmbHalal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cmbTax, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cmbHalal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9)))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(txtBarcode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(txtPLU, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel4)))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(txtNama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel6)))
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(txtPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(txtKategori, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel1)
                                .addComponent(jLabel3))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(txtSupplier, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel2))
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(cmbTag, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel7))))))
                .addContainerGap())
        );

        jSplitPane1.setTopComponent(jPanel1);

        grid.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(grid);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 779, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
        );

        jSplitPane1.setRightComponent(jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 781, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 261, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        new ItemFrm().execute(0);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        loadData();
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        updateGrid();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        initFilter();
    }//GEN-LAST:event_btnResetActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnReset;
    private javax.swing.JComboBox cmbHalal;
    private javax.swing.JComboBox cmbTag;
    private javax.swing.JComboBox cmbTax;
    private javax.swing.JTable grid;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTextField txtBarcode;
    private javax.swing.JTextField txtKategori;
    private javax.swing.JTextField txtNama;
    private javax.swing.JTextField txtPLU;
    private javax.swing.JTextField txtPrincipal;
    private javax.swing.JTextField txtSupplier;
    // End of variables declaration//GEN-END:variables

    @Override
    public void execute(boolean withVisible, int id) {
         initGrid();
         performList.add(0, new NameValueList("ALL", "All"));
         cmbTag.setModel(ComponentUtils.getDefaultComboBoxModel(performList));
         initFilter();
         if (withVisible) { setVisible(true); }
    }

    @Override
    public void initGrid() {
        grid.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        tableModel = new ItemTM();
        grid.setModel(tableModel);
        
        DefaultTableCellRenderer rightCellRender = new DefaultTableCellRenderer();
        rightCellRender.setHorizontalAlignment(SwingConstants.RIGHT);
        grid.getColumnModel().getColumn(COL_NO).setCellRenderer(rightCellRender);
        grid.getColumnModel().getColumn(COL_MAX).setCellRenderer(rightCellRender);
        grid.getColumnModel().getColumn(COL_MIN).setCellRenderer(rightCellRender);
        grid.getColumnModel().getColumn(COL_MINX).setCellRenderer(rightCellRender);
        grid.getColumnModel().getColumn(COL_NO).setMaxWidth(70);
        
        ColumnResizer.adjustColumnPreferredWidths(grid);
        sorter = new TableRowSorter<>(tableModel);
        grid.setRowSorter(sorter);
        grid.setGridColor(Color.LIGHT_GRAY);
        
        setTableLookStyle();
    }

    @Override
    public void initFilter() {
        cmbHalal.removeItemListener(comboboxListener);
        cmbTag.removeItemListener(comboboxListener);
        cmbTax.removeItemListener(comboboxListener);
        txtBarcode.setText("");
        txtKategori.setText("");
        txtNama.setText("");
        txtPLU.setText("");
        txtPrincipal.setText("");
        txtSupplier.setText("");
        cmbHalal.setSelectedIndex(0);
        cmbTag.setSelectedIndex(0);
        cmbTax.setSelectedIndex(0);
        
        dept = null;
        supplier = null;
        principal = null;
        cmbHalal.addItemListener(comboboxListener);
        cmbTag.addItemListener(comboboxListener);
        cmbTax.addItemListener(comboboxListener);
    }

    public Map<NameValueList, Object> getFilter(){
        Map<NameValueList, Object> params = new HashMap<>();
        if(!"".equals(txtNama.getText())) { 
            params.put(new NameValueList("item.nama", Constants.OPERATOR_LIKE), "%"+txtNama.getText()+"%"); }

        if(!"".equals(txtPLU.getText())) { 
            params.put(new NameValueList("item.plu", Constants.OPERATOR_EQUAL), txtPLU.getText()); }
        
        if(!"".equals(txtBarcode.getText())) { 
            params.put(new NameValueList("item.barcode", Constants.OPERATOR_EQUAL), txtBarcode.getText()); }
         
        if(principal!=null) { 
            params.put(new NameValueList("item.principal.id", Constants.OPERATOR_EQUAL), principal.getId()); }
         
        if(dept!=null) { 
            params.put(new NameValueList("item.dept.id", Constants.OPERATOR_EQUAL), dept.getId()); }
        
        String tag = ((NameValueList) cmbTag.getSelectedItem()).getKey();
        if (!"ALL".equals(tag)) {
             params.put(new NameValueList("item.performa", Constants.OPERATOR_EQUAL), tag); }
         
        switch (cmbHalal.getSelectedIndex()) {
             case 1: params.put(new NameValueList("item.halal", Constants.OPERATOR_EQUAL), true); break;
             case 2: params.put(new NameValueList("item.halal", Constants.OPERATOR_EQUAL), false); break;
        }
        
        switch (cmbTax.getSelectedIndex()) {
             case 1: params.put(new NameValueList("item.bkp", Constants.OPERATOR_EQUAL), true); break;
             case 2: params.put(new NameValueList("item.bkp", Constants.OPERATOR_EQUAL), false); break;
        }
         
        if(supplier!=null) { 
            params.put(new NameValueList(null, Constants.OPERATOR_CUSTOM), 
            "(item.id in (select it.id.itemId from ItemSupplier it where item.id = it.id.itemId and it.id.supplierId="+supplier.getId()+"))");
        }
        
        params.put(new NameValueList(null, Constants.OPERATOR_ORDERBY), "item.nama"); 
         
        return params;
    }
    
    @Override
    public void loadData() {
        tableModel.clear();
        items =  ServicesList.getItemService().getItem(0, 100000, getFilter());
        tableModel.add(items);
        ColumnResizer.adjustColumnPreferredWidths(grid);
    }

    @Override
    public void updateGrid() {
        //DBTable qtable = new DBTable();
        int row = grid.getSelectedRow();
        if (row > -1) {
            if(items.get(row) != null) {
                new ItemFrm().execute(items.get(row).getId());
                Item updatedData = ServicesList.getItemService().getItem(items.get(row).getId());
                tableModel.set(row, updatedData);
            }
        }
    }

    @Override
    public void insertGrid() {
        int itemId = new ItemFrm().execute(0);
        if (itemId>0) {
            Item insertedData = ServicesList.getItemService().getItem(itemId);
            tableModel.add(insertedData);
        }
    }
    
    private void setTableLookStyle() {
        
        try {
            
            header = grid.getTableHeader().getDefaultRenderer();
            if (header!=null) {
                grid.getTableHeader().setDefaultRenderer(new TableCellRenderer() {
                    @Override
                    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                        JLabel lbl = (JLabel) header.getTableCellRendererComponent(table, 
                              value, isSelected, hasFocus, row, column);
                        lbl.setBorder(BorderFactory.createEmptyBorder(5,1,5,1));   

                        switch (column) {
                            case COL_NO: lbl.setHorizontalAlignment(SwingConstants.RIGHT); break;
                            case COL_MAX: lbl.setHorizontalAlignment(SwingConstants.RIGHT); break; 
                            case COL_MIN: lbl.setHorizontalAlignment(SwingConstants.RIGHT); break;
                            case COL_MINX: lbl.setHorizontalAlignment(SwingConstants.RIGHT); break;
                            default: lbl.setHorizontalAlignment(SwingConstants.LEFT); break;   
                        }

                        if (isSelected) {
                            lbl.setForeground(Color.red);
                            lbl.setBackground(Color.lightGray);
                        } else {
                            lbl.setForeground(Color.WHITE);
                            lbl.setBackground(Color.DARK_GRAY);
                        }
                        return lbl;
                    }

                });
            }
        } catch (Exception e) {}
    }
}
